<template>
	<view class="group-detail-wrapper relative" v-if="groupProfile">
		<view class="header clear">
			<template v-for="member in memberList">
				<view class="member pull-left" :key="member.userID" @click="toUserProfile(member)">
					<image class="avatar" :src="member.avatar || '/static/images/head.png'" />
					<view class="name">
						{{member.nameCard || member.nick || member.userID}}
					</view>
				</view>
			</template>
		</view>
		<!-- 群组相关资料 -->
		<view class="notification-wrap bg-white" @click="handleOpenForm">
			<view class="title">群公告</view>
			<view class="notification flex-x-between">
				<text style="width: 95%;" class="text u-line-1">{{groupProfile.notification || '暂无'}}</text>
				<text class="iconfontPT iconxiayibu" v-if="isOwner"></text>
			</view>
		</view>
		<!-- 群组相关操作 -->
		<view class="group-action-wrap bg-white text-center">
			<view class="cancel theme" @click="handleQuit">
				{{isOwner? '解散并退出群聊' : '退出群聊' }}
			</view>
		</view>
		<view class="form bg-white" v-if="showForm">
			<view class="textarea-content">
				<textarea placeholder="输入群公告" class="w100" auto-height :fixed="true" maxlength="300" cursor-spacing="20"
				 :show-confirm-bar="false" v-model="form.content" confirm-type="send" auto-focus></textarea>
			</view>
			<cover-view class="confirm-btn-wrap w100 bg-white">
				<button class="confirm-btn" hover-class="hoverClass" @click="handleConfirm">
					发布公告
				</button>
			</cover-view>
		</view>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { outGroup ,deleteGroup } from '../../../apis/life-circle/index';
	export default {
		data() {
			return {
				showForm: false,
				form: {
					content: ''
				},
				rules: {
					content: [{
							required: true,
							message: '请输入群公告',
						},
						{
							min: 2,
							max: 300,
							message: '群公告字符长度在 2 到 300 个字符',
						}
					]
				},
				oid:''
			}
		},
		onLoad(option) {
			this.oid = option.oid
			uni.$Tim.getGroupMemberList({
					groupID: this.groupProfile.groupID
				})
				.then(({
					data: {
						memberList
					}
				}) => {
					this.$store.commit('tim/UPDATE_CURRENT_GROUPME_MBERLIST', memberList)
					this.form.content = this.groupProfile.notification
				})

		},
		onShow() {
			this.showForm = false
		},
		onUnload() {
			this.$store.commit('tim/RESET_CURRENT_MEMBER_LIST')
		},
		computed: {
			...mapGetters(['groupProfile', 'memberList']),
			isOwner(){
				return this.groupProfile.selfInfo.role === 'Owner'
			}
		},
		methods: {
			handleOpenForm() {
				if(this.isOwner){
					this.showForm = true
				}				
			},
			toUserProfile(member) {
				// uni.navigateTo({
				// 	url: `../user-profile/main?userID=${member.userID}`
				// })
			},
			handleConfirm() {
				this.$verification(this.form, this.rules, ok => {
					if (ok) {
						uni.$Tim.updateGroupProfile({
							groupID: this.groupProfile.groupID,
							notification: this.form.content
						}).then(() => {
							uni.showToast({title: '编辑成功！',duration: 600})
							this.showForm = false
						})
					}
				})

			},
			async handleQuit() {
				// const isOwner = this.groupProfile.type !== uni.TIM.TYPES.GRP_PRIVATE && this.groupProfile.selfInfo.role === 'Owner'
				
				if(this.isOwner){
					const [,{confirm}] = await uni.showModal({title: '提示',content:'是否解散群聊？'})
					if(confirm){
						try{
							const {code,data,info} = await deleteGroup({oid:this.oid})
							if(code === 200){
								await uni.$Tim.dismissGroup(this.groupProfile.groupID)
								await uni.showToast({title: '解散成功！'})
								uni.switchTab({url: '/pages/life-circle'})		
							}else{
								uni.showModal({ title:'提示',content:info,showCancel:false})
							}					
						}catch(e){
							uni.showModal({ title:'提示',content:e.message,showCancel:false})
						}
					}
				}else{
					const [,{confirm}] = await uni.showModal({title: '提示',content:'是否退出群聊？'})
					if(confirm){
						try{
							const {code,data,info} = await outGroup({oid:this.oid})
							if(code === 200){
								await uni.$Tim.quitGroup(this.groupProfile.groupID)
								this.$store.commit('tim/CANCEL_GROUP', this.groupProfile.groupID)
								await uni.showToast({title: '退群成功！'})
								uni.switchTab({url: '/pages/life-circle'})	
							}else{
								uni.showModal({ title:'提示',content:info,showCancel:false})
							}	
						}catch(e){
							uni.showModal({ title:'提示',content:e.message,showCancel:false})
						}
					}
				}
			},
		}
	}
</script>

<style lang="less">
	.group-detail-wrapper {
		height: 100vh;
		background-color: #ededed;

		.header {
			height: 500rpx;
			overflow-y: scroll;
			background-color: #fff;
			padding-bottom: 36rpx;
			margin-bottom: 20rpx;

			.member {
				display: flex;
				justify-content: space-between;
				align-items: center;
				flex-direction: column;
				width: 20%;
				padding-top: 32rpx;

				.name {
					width: 80rpx;
					font-size: 24rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					text-align: center;
				}

				.avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 5rpx
				}
			}
		}

		.notification-wrap {
			padding: 25rpx;
			box-sizing: border-box;

			.title {
				margin-bottom: 20rpx;
			}

			.notification {
				font-size: 26rpx;
				color: #989898;
			}
		}

		.group-action-wrap {
			margin-top: 20rpx;
			padding: 25rpx;
			box-sizing: border-box;

			.cancel {
				padding: 15rpx 0;
			}
		}

		.form {
			width: 100%;
			height: 100vh;
			position: absolute;
			top: 0;
			left: 0;
			padding: 30rpx;
			box-sizing: border-box;

			.confirm-btn-wrap {
				position: fixed;
				bottom: 0;
				left: 0;
				box-shadow: 0rpx -2rpx 8rpx 0rpx rgba(0, 0, 0, 0.1);
				height: 166rpx;

				.confirm-btn {
					width: 690rpx;
					margin-top: 15rpx;
					background: #E41805FF;
					color: #ffffff;
					font-size: 30rpx;

				}
			}
		}
	}

	.group-action {
		margin-top: 16rpx;
	}

	.quit {
		color: #fb5250;
	}
</style>
